# 导包
import unittest
from interfaceDatabase.api.login import LoginAPI
from parameterized import parameterized
from interfaceDatabase.tools.dbutil import DBUtil
from interfaceDatabase.uitls import common_assert

# 构造测试数据,基于数据库实现参数化
def build_data():
    sql = "select * from t_login"
    db_data =DBUtil.exe_sql(sql)
    print(db_data)
    test_data = []
    for case_data in db_data:
        username = case_data[0]
        password = case_data[1]
        status_code = case_data[2]
        # print(status_code)
        message = case_data[3]
        code = case_data[4]
        test_data.append((username, password, status_code, message, code))
        print("test_data=".format(username, password, status_code, message, code))
    return test_data



# 定义测试类
class TestLoginAPI2(unittest.TestCase):
    # 前置处理
    def setUp(self):
        self.login_api = LoginAPI()

    # 后置处理
    def tearDown(self):
        print("end")

    # 定义测试方法
    # 登录成功
    @parameterized.expand(build_data())
    def test001_login(self,username,password,status_code,message,code):
        respones = self.login_api.login(username,password)
        print(respones.status_code)

        common_assert(self,respones,status_code,message,code)
        # self.assertEqual(status_code,respones.status_code)
        # self.assertIn(message,respones.json().get('message'))
        # self.assertEqual(code,respones.json().get('code'))


